package test_achieve.courierapp;

import constant.Constant;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import test_achieve.Hooks;
import test_achieve.admin.HttpAdmin;
import util.ConfigSql;
import util.HttpClient;
import util.JdbcUtil;
import util.tools.BusinessTools;
import util.grepkey.EncryptManager;
import util.grepkey.GrabEncryptStrategy;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenkuo on 2017/11/15.
 */


/**获取Unix时间戳（毫秒）*/
public class HttpCourier {
    private static String unixTimeMs(){
        long now = System.currentTimeMillis();
//        System.out.println(now);
        String grabDate  = Long.toString(now);
        return grabDate;
    }



    /**获取订单的taskNumber*/
    private static String getTaskNumber(String orderNumber){
        String orderId = JdbcUtil.sqlString(ConfigSql.get("queryOrderId",orderNumber));
        String taskNumber =JdbcUtil.sqlString(ConfigSql.get("queryTaskNumber",orderId));
        return taskNumber;
    }

    /**获取闪送员token*/
    public static String gettoken(String user) throws Exception {
        String token = JdbcUtil.sqlString(ConfigSql.get("queryUser2CourierToken",user));
        if (token==null){
             token = JdbcUtil.sqlString(ConfigSql.get("queryUser1CourierToken",user));
        }
        System.out.println("token:"+token);
        if(token==null){
            throw new Exception("token为空，请重新登录闪送员生成token");
        }
        return token;
    }


    /**公共参数*/
    private static void addComm(Map<String, String> map,String user) throws Exception {
        String grabDate = unixTimeMs();
        String orderNumber =Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        map.put("login","1");
        map.put("timestamp",grabDate);
        map.put("timestamp_log","1510616890740");
        map.put("imei_log","867323027535085");
        map.put("pss","0");//
        map.put("channel","CHSDY");
        map.put("token", gettoken(user));
        map.put("cityCode","010");
        map.put("acceptOrderType","0");
        map.put("iss_from","android_ssy");
        map.put("isApp","1");
        map.put("shansongId","731fd36ead2d99e0658531597b8dab2f");
        map.put("version","308");
        map.put("apiVersion","v10");
        //除了8种基本类型之外，都可以这样用，参数传递是对象也可以
        //上面是冀龙指定公共参数，下面是抽取出来的
        map.put("user",user);
        map.put("orderNumber",orderNumber);
    }

    @Given("^p抢单成功：(.+)$")
    public  void p抢单成功(String user) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
//        buidleParm();
//        sendRequst();
        String dispatchOrderNum = BusinessTools.getDispatchOrderNum(userSecret);
        if (dispatchOrderNum!=null){
            System.out.println("此闪送员身上有订单："+dispatchOrderNum+"  请流单后再测试");
            HttpAdmin httpAdmin = new HttpAdmin();
            httpAdmin.p订单因其他客服流单(dispatchOrderNum);
        }
        String url = Constant.grabUrl;
        System.out.println("抢单接口:"+url);
        String orderNumber =Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        String orderId = JdbcUtil.sqlString(ConfigSql.get("queryOrderId",orderNumber));
        System.out.println("抢单单号："+orderNumber);
        String grabDate  = unixTimeMs();
        String grabKey = new EncryptManager(new GrabEncryptStrategy()).secure(orderNumber, String.valueOf(grabDate));
        HttpClient httpClient = new HttpClient();
        Map<String, String> map = new HashMap<String, String>();
        map.put("orderId",orderId);
        map.put("grabDate",grabDate);
        map.put("dis_grab","0.0");
        map.put("key",grabKey);
        map.put("orderLat","39.983255");
        map.put("orderLng","116.400663");
        map.put("locflag","1");
        map.put("lat","39.983266");
        map.put("lng","116.400668");
        map.put("speed","0.0");
        map.put("locType","7.2.8");
        addComm(map,userSecret);
        httpClient.post(url,map);
        for (int i = 0; i < 6; i++) {
            int orderStatu = JdbcUtil.sqlInt(ConfigSql.get("queryorder2status",orderNumber));
            if (orderStatu ==30){
                break;
            }else {
                Thread.sleep(3000);
            }
        }
        BusinessTools.checkOrderStatus(orderNumber,30);

    }

    @Given("^p闪送员致电寄方：(.+)$")
    public void p闪送员致电寄方(String user) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        String url = Constant.callUrl;
        String orderNumber =Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        HttpClient httpClient = new HttpClient();
        Map<String, String> map = new HashMap<String, String>();
        addComm(map,userSecret);
        map.put("taskNumber",getTaskNumber(orderNumber));
        map.put("status","40");
        map.put("orderId","0");
        map.put("taskId","0");
        map.put("versionname","7.2.8");
        httpClient.post(url,map);
        BusinessTools.checkOrderStatus(orderNumber,40);
    }

    @Given("^p闪送员同步订单：(.+)$")
    public void p闪送员同步订单(String user) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        String url = "http://api.ss.bingex.com/courier/api/order/call";
        HttpClient httpClient = new HttpClient();
        Map<String, String> map = new HashMap<String, String>();
        addComm(map,userSecret);
        map.put("time","1510801723978");
        map.put("clientData","orderNumber:TDH2017111510481884&task_id:12673053&task_status:40");
        map.put("versionname","7.2.8");
        httpClient.post(url,map);

    }

    @Given("^p闪送员已就位：(.+)$")
    public void p闪送员已就位(String user) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        String url = Constant.pickupPrepareUrl;
        String orderNumber =Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        HttpClient httpClient = new HttpClient();
        Map<String, String> map = new HashMap<String, String>();
        addComm(map,userSecret);
        map.put("taskNumber",getTaskNumber(orderNumber));
        map.put("orderId","0");
        map.put("taskId","0");
        map.put("lat","40.054647");
        map.put("lng","116.289135");
        map.put("speed","0.0");
        map.put("locType","161");
        httpClient.post(url,map);
        BusinessTools.checkOrderStatus(orderNumber,42);
    }

    @Given("^p闪送员完成取件：(.+)$")
    public void p闪送员完成取件(String user) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        String url = Constant.pickupUrl;
        System.out.println("取件URL："+url);
        String orderNumber =Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        String pwd =JdbcUtil.sqlString(String.valueOf(ConfigSql.get("queryPickupPwd",orderNumber)));//获取订单取件密码
        System.out.println("取件密码是："+pwd);
        HttpClient httpClient = new HttpClient();
        Map<String, String> map = new HashMap<String, String>();
        addComm(map,userSecret);
        map.put("taskNumber",getTaskNumber(orderNumber));
        map.put("orderId","0");
        map.put("pickupDate",unixTimeMs());
        map.put("pwd",pwd);
        map.put("taskId","0");
        map.put("lat","40.054647");
        map.put("lng","116.289135");
        map.put("locflag","1");
        map.put("speed","0.0");
        map.put("locType","161");
        httpClient.post(url,map);
        BusinessTools.checkOrderStatus(orderNumber,44);
    }

    @Given("^p闪送员致电收方：(.+)$")
    public void p闪送员致电收方(String user) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        String url = Constant.callUrl;
        String orderNumber =Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        HttpClient httpClient = new HttpClient();
        Map<String, String> map = new HashMap<String, String>();
        map.put("taskNumber",getTaskNumber(orderNumber));
        map.put("status","52");
        map.put("orderId","0");
        map.put("taskId","0");
        map.put("versionname","7.2.8");
        addComm(map,userSecret);
        httpClient.post(url,map);
        BusinessTools.checkOrderStatus(orderNumber,52);
    }

    @Given("^p闪送员完成收件：(.+)$")
    public void p闪送员完成收件(String user) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        String url = Constant.deliveryUrl;
        String orderNumber =Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        String pwd = JdbcUtil.sqlString(ConfigSql.get("queryDeliveryPwd",orderNumber));
        HttpClient httpClient = new HttpClient();
        Map<String, String> map = new HashMap<String, String>();
        addComm(map,userSecret);
        map.put("taskNumber",getTaskNumber(orderNumber));
        map.put("orderId","0");
        map.put("taskId","0");
        map.put("deliveryDate",unixTimeMs());
        map.put("pwd",pwd);
        map.put("locflag","1");
        map.put("lat","40.054647");
        map.put("lng","116.289135");
        map.put("versionname","7.2.8");
        httpClient.post(url,map);
        BusinessTools.checkOrderStatus(orderNumber,60);
    }

    @Given("^p闪送员完成评价：(.+)：(.+)$")
    public void p闪送员完成评价闪送员账户(String user,String num) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        num = num == null ? "5" : num;
        String url = Constant.ratingUserUrl;
        String orderNumber =Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        String orderId = JdbcUtil.sqlString(ConfigSql.get("queryOrderId",orderNumber));
        System.out.println("orderId:"+orderId);
        Map<String, String> map = new HashMap<String, String>();
        addComm(map,userSecret);
        map.put("rating_number",num);
        map.put("rating_lable","");
        map.put("rating_comment","");
        map.put("rating_user","15501213579");
        map.put("orderId",orderId);
        HttpClient.post(url,map);
    }

    @Given("^p闪送员心跳：(.+)：(.+)：(.+)：(.+)：(.+)$")
    public void p闪送员心跳(String user,int num,int time,String precision,String latitude) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        String url = Constant.courierHeartbeat;
        String grabDate = unixTimeMs();
        Map<String, String> map = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();

        builder.append("{").
                append("login:").append("1").append("t").append("}");

        map.put("login","1");
        map.put("timestamp",grabDate);
        map.put("timestamp_log","1510616890740");
        map.put("imei_log","867323027535085");
        map.put("pss","0");//
        map.put("channel","CHSDY");
        map.put("token", gettoken(userSecret));
        map.put("cityCode","010");
        map.put("acceptOrderType","0");
        map.put("iss_from","android_ssy");
        map.put("isApp","1");
        map.put("shansongId","731fd36ead2d99e0658531597b8dab2f");
        map.put("version","308");
        map.put("apiVersion","v10");
        map.put("user",user);
        map.put("acceptOrderType","1");
        map.put("_obj","heartbeat");
        map.put("_tp","null");
        map.put("net","wifi");
        map.put("gps","1");
        map.put("locflag","1");
        map.put("citycode","131");
        map.put("lat",precision);//精度
        map.put("lng",latitude);//纬度
        map.put("_act","success");
        map.put("grabStatus","1");//闪送员类型  0 推单 1 派单
        map.put("versionname","7.3.6");
        map.put("_param","versionname:7.3.6&version:316&speed:0.0&locflag:1&citycode:131&lat:"+precision+"&locType:61&lng:"+latitude+"");
        for (int i = 0; i < num; i++) {
            Thread.sleep(time*1000);
            HttpClient.post(url,map);
        }

    }

    @Given("^p闪送员打开听单：(.*)：(.*)：(.*)$")
    public void p闪送员打开听单(String user,String word,String type) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        String url = Constant.courierOpenOffWork;
        String grabDate = unixTimeMs();
        Map<String, String> map = new HashMap<String, String>();
        map.put("token", gettoken(userSecret));
        map.put("user",userSecret);
        map.put("login","1");
        map.put("timestamp",grabDate);
        map.put("imei_log","867323027535085");
        map.put("pss","0");
        map.put("channel","CHSDY");
        map.put("cityCode","010");
        map.put("acceptOrderType","0");
        map.put("iss_from","android_ssy");
        map.put("isApp","1");
        map.put("shansongId","731fd36ead2d99e0658531597b8dab2f");
        map.put("work",word);//1是听单 0是不听单
        map.put("orderType",type);//orderType=1是即时单，2是预约单，3是全部订单，0关闭听单
        map.put("orders","");
        map.put("versionname","7.3.6");
        map.put("version","308");
        map.put("apiVersion","v10");
        HttpClient.post(url,map);
    }

    @Given("^接口流单：(.*)$")
    public void 接口流单订单号(String order) throws Throwable {
        Constant.OrderNumbuter.add(order);
        Hooks hooks = new Hooks();
        hooks.cancelOrder();
    }

    @Given("^p闪送员确定用户取消订单：(.*)$")
    public void p闪送员确定用户取消订单(String user) throws Throwable {
        String orderNumber =Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        String orderId = JdbcUtil.sqlString(ConfigSql.get("queryOrderId",orderNumber));
        String userSecret = BusinessTools.getMobileSecret(user);
        String url = Constant.courierAgreeCancel;
        Map<String, String> map = new HashMap<String, String>();
        addComm(map,userSecret);
        map.put("user",user);
        map.put("channel","shansong");
        map.put("shansongId","e021083d1868cc6df927e0863394037f");
        map.put("orderId","");
        map.put("isAgree","1");
        map.put("lat","40.054387");//精度
        map.put("lng","116.289743");//纬度
        map.put("versionname","7.5.2");
        map.put("orderId",orderId);
        HttpClient.post(url,map);
    }
}
